<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_PageLoader
{
    var $Controller;
    var $Group;
    var $Request;
    var $Page;
    var $Path;
    var $Pane;
    var $Seek;
    var $Index;
    function News_Command_PageLoader($controller)
	{
	    $this->Controller = $controller;
        $this->Group      = $this->Controller->Settings['group'];
        $this->Request    = $this->Controller->Settings['request'];
        $this->Page       = $this->Controller->Settings['page'];
        $this->Pane       = $this->Controller->Settings['pane'];
        $this->Seek       = $this->Controller->Settings['seek'];
        $this->Up         = $this->Controller->Settings['up'];
        $this->Down       = $this->Controller->Settings['dn'];
        $this->Index      = $this->Controller->Settings['index'];
        $this->Path       = "notify/{$this->Group}.{$this->Page}.page";
	    if ($this->Request=='browse')
	    {
	        $this->Path = "notify/{$this->Group}.{$this->Index}.{$this->Pane}.browse";
	    }
	    if ($this->Request=='hilo')
	    {
	        $this->Path = "notify/{$this->Group}.{$this->Index}.{$this->Pane}-{$this->Down}-{$this->Up}.hilo";
	    }
        $this->URI        = $this->Controller->URI (array('status'=>'ok'));
	}
	
	function GetURI()
	{
	    $uri = array('ui'=>'articlelist', 'request'=>NULL);
	    if ($this->Request=='hilo')
	    {
	        $uri['view'] = 'hilo';
	    }
	    $uri = $this->Controller->URI ($uri);
		if (isset($this->Controller->Settings['response']))
		{ 
		    $uri = "/x/groupname/{$this->Group}/pageindex/{$this->Page}";	
		}
		
		
	    return array("RequestURI"  => $this->URI,
	                 "RedirectURI" => $uri); 
	}
	
	function GetStatus()
	{ 
	    if (!file_exists($this->Path))
        {
            return 'OK';
        } 
        return '';
	}
	
	function Signal()
	{   
        $message = $this->Group;
        
        if (strlen($this->Controller->Settings['seek']) > 0)
        {
            $message = "groupname={$this->Group}&filename=data/find/{$this->Group}/{$this->Seek}.xml";
        } 
        if (strlen($this->Controller->Settings['status']) > 0)
        {
            return $this->GetStatus ();
        } 
        
        $this->Controller->Save($this->Path, $message); 
        return $this->Controller->Dialog ($this->GetURI());
	}
	
	function Invoke($command)
	{ var_dump ($command);
        $pageindex  = $command['arg1'];
        $groupname  = $this->Controller->Read($command['path']); 
        $this->Path = $command['path'];
        $filename   = "data/{$groupname}.xml";
		if (strpos($groupname, '&')) parse_str($groupname);
        if (file_exists($filename)) 
        {
        	$bytes = filesize ($filename);
        	echo "Loading {$bytes} bytes from {$filename}...\n\n";
		    $xml = simplexml_load_file($filename);
		    if ($xml)
		    {
		        if ($command['type']=='hilo')
		        {
		            $itemindex = $command['arg2']; $tmp = explode ('-', $pageindex);
		            $result = $xml->xpath("//list/article[@index='{$itemindex}']/items/article");
	                    $dn = $tmp[1]; $up = $tmp[2]; $ret=array();
	                    $matrix = $this->Controller->CreateHiLo ($dn, $up);
	                    $result = $this->Controller->SortArticles ($result);
	
	                     echo $dn . "x" . $up;
	                     for($i=0;$i<sizeof($matrix);$i++)
	                     {
	                         $x = $matrix[$i];
	
	                     	 echo " ---> {$x}\n";
	                         $ret[]=$result[$x]; 
	                     }
	                     $this->Controller->Preload ($groupname, $ret, -2);  
                         unlink ($this->Path);
		                 return;
		        }
				else if ($command['type']=='browse')
		        {
		            $itemindex = $command['arg2'];
			        $result = $xml->xpath("//list/article[@index='{$itemindex}']/items/article");
		        }
		        else
		        { 
			        echo "Loading articles...\n\n";
			        $result = $xml->xpath("//list/article");
			        $result = $this->Controller->SortArticles($result);
					//usort($result, 'cmpx');
		        }
			    echo "Calling Controller...\n\n";
		        $this->Controller->Preload ($groupname, $result, $pageindex); 
		    } 
			else {
			    echo " *** ERROR: Could not open ($filename)\n";	
			}
        }   
        unlink ($this->Path);
	}
}



